<template>
    <div class="app-container calendar-list-container">
        <div class="filter-container">
            <el-button v-if="training_made_up_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加
            </el-button>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
                  highlight-current-row style="width: 100%">
            <el-table-column align="center" label="" v-if="dialogStatus == 'update'">
                <template scope="scope">
                    <span>{{ scope.row.fid }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="科目">
                <template scope="scope">
                    <span>{{ scope.row.subjectId }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="补考费">
                <template scope="scope">
                    <span>{{ scope.row.amount }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="学员">
                <template scope="scope">
                    <span>{{ scope.row.studentId }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="是否支付">
                <template scope="scope">
                    <span>{{ scope.row.isPay }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="支付方式">
                <template scope="scope">
                    <span>{{ scope.row.payType }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="考场">
                <template scope="scope">
                    <span>{{ scope.row.madeRoom }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="预约考试日期">
                <template scope="scope">
                    <span>{{ scope.row.appointmentDate }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="状态">
                <template scope="scope">
                    <span>{{ scope.row.bizStatus }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200">
                <template scope="scope">
                    <el-button v-if="training_made_up_upd" size="mini" type="success"
                               @click="handleUpdate(scope.row)">编辑
                    </el-button>
                    <!--<el-button v-if="training_made_up_del" size="mini" type="danger"-->
                               <!--@click="handleDelete(scope.row)">删除-->
                    <!--</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page.sync="listQuery.page"
                           :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="cancel('form')">
            <el-form :model="form" :rules="rules" ref="form" size="mini" label-width="100px">
                        <el-form-item label="编码" prop="fid" v-if="dialogStatus == 'update'">
                            <el-input v-model="form.fid" placeholder="" :disabled="true"></el-input>
                        </el-form-item>
                        <el-form-item label="科目" prop="subjectId">
                            <el-input v-model="form.subjectId" placeholder="科目"></el-input>
                        </el-form-item>
                        <el-form-item label="补考费" prop="amount">
                            <el-input v-model="form.amount" placeholder="补考费"></el-input>
                        </el-form-item>
                        <el-form-item label="学员" prop="studentId">
                            <el-input v-model="form.studentId" placeholder="学员"></el-input>
                        </el-form-item>
                        <el-form-item label="是否支付" prop="isPay">
                            <el-input v-model="form.isPay" placeholder="是否支付"></el-input>
                        </el-form-item>
                        <el-form-item label="支付方式" prop="payType">
                            <el-input v-model="form.payType" placeholder="支付方式"></el-input>
                        </el-form-item>
                        <el-form-item label="考场" prop="madeRoom">
                            <el-input v-model="form.madeRoom" placeholder="考场"></el-input>
                        </el-form-item>
                        <el-form-item label="预约考试日期" prop="appointmentDate">
                            <el-input v-model="form.appointmentDate" placeholder="预约考试日期"></el-input>
                        </el-form-item>
                        <el-form-item label="状态" prop="bizStatus">
                            <el-input v-model="form.bizStatus" placeholder="状态"></el-input>
                        </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('form')">取 消</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
                <el-button v-else type="primary" @click="update('form')">修 改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchList, addObj, putObj, delObj } from '@/api/training/training_made_up'
    import waves from '@/directive/waves/index.js' // 水波纹指令
    import { mapGetters } from 'vuex'

    export default {
        name: 'table_training_made_up',
        directives: {
            waves
        },
        data() {
            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    limit: 20
                },
                rules: {
                    fid: [
                        { required: true, message: '请输入', trigger: 'blur' }
                    ],
                    subjectId: [
                        { required: true, message: '请输入科目', trigger: 'blur' }
                    ],
                    amount: [
                        { required: true, message: '请输入补考费', trigger: 'blur' }
                    ],
                    studentId: [
                        { required: true, message: '请输入学员', trigger: 'blur' }
                    ],
                    isPay: [
                        { required: true, message: '请输入是否支付', trigger: 'blur' }
                    ],
                    payType: [
                        { required: true, message: '请输入支付方式', trigger: 'blur' }
                    ],
                    madeRoom: [
                        { required: true, message: '请输入考场', trigger: 'blur' }
                    ],
                    appointmentDate: [
                        { required: true, message: '请输入预约考试日期', trigger: 'blur' }
                    ],
                    bizStatus: [
                        { required: true, message: '请输入状态', trigger: 'blur' }
                    ],
                    createUser: [
                        { required: true, message: '请输入创建人', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '请输入创建时间', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '请输入最后修改时间', trigger: 'blur' }
                    ],
                    updateUser: [
                        { required: true, message: '请输入最后修改人', trigger: 'blur' }
                    ],
                    version: [
                        { required: true, message: '请输入版本', trigger: 'blur' }
                    ],
                    delFlag: [
                        { required: true, message: '请输入删除标识', trigger: 'blur' }
                    ],
                },
                form: {
                    fid: '',
                    subjectId: '',
                    amount: '',
                    studentId: '',
                    isPay: '',
                    payType: '',
                    madeRoom: '',
                    appointmentDate: '',
                    bizStatus: '',
                    createUser: '',
                    createTime: '',
                    updateTime: '',
                    updateUser: '',
                    version: '',
                    delFlag: '',
                },
                dialogFormVisible: false,
                dialogStatus: '',
                    training_made_up_add: false,
                    training_made_up_upd: false,
                    training_made_up_del: false,
                textMap: {
                    update: '编辑',
                    create: '创建'
                },
                tableKey: 0
            }
        },
        computed: {
            ...mapGetters([
                'permissions'
            ])
        },
        filters: {
            statusFilter(status) {
                const statusMap = {
                    1: '有效',
                    0: '无效'
                }
                return statusMap[status]
            }
        },
        created() {
            this.getList()
            this.training_made_up_add = this.permissions['training_made_up_add']
            this.training_made_up_upd = this.permissions['training_made_up_upd']
            this.training_made_up_del = this.permissions['training_made_up_del']
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.records
                    this.total = response.data.total
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.limit = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleDelete(row) {
                delObj(row)
                        .then(response => {
                            this.dialogFormVisible = false
                            this.getList()
                            this.$notify({
                                title: '成功',
                                message: '删除成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
            },
            handleUpdate(row) {
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                   this.form.fid = row.fid
                   this.form.subjectId = row.subjectId
                   this.form.amount = row.amount
                   this.form.studentId = row.studentId
                   this.form.isPay = row.isPay
                   this.form.payType = row.payType
                   this.form.madeRoom = row.madeRoom
                   this.form.appointmentDate = row.appointmentDate
                   this.form.bizStatus = row.bizStatus
                   this.form.createUser = row.createUser
                   this.form.createTime = row.createTime
                   this.form.updateTime = row.updateTime
                   this.form.updateUser = row.updateUser
                   this.form.version = row.version
                   this.form.delFlag = row.delFlag
            },
            handleCreate() {
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            create(formName) {
                const set = this.$refs
                        set[formName].validate(valid => {
                    if (valid) {
                        addObj(this.form)
                                .then(() => {
                                    this.dialogFormVisible = false
                                    this.getList()
                                    this.$notify({
                                        title: '成功',
                                        message: '创建成功',
                                        type: 'success',
                                        duration: 2000
                                    })
                                    const set = this.$refs
                                    set[formName].resetFields()
                                })
                    } else {
                        return false
                    }
                })
            },
            cancel(formName) {
                this.dialogFormVisible = false
                const set = this.$refs
                set[formName].resetFields()
            },
            update(formName) {
                const set = this.$refs
                        set[formName].validate(valid => {
                    if (valid) {
                        this.dialogFormVisible = false
                        this.form.password = undefined
                        putObj(this.form).then(() => {
                            this.dialogFormVisible = false
                            this.getList()
                            const set = this.$refs
                            set[formName].resetFields()
                            this.$notify({
                                title: '成功',
                                message: '修改成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
                    } else {
                        return false
                    }
                })
            }
        }
    }
</script>
